import { createStore } from 'vuex'
import persist from 'vuex-persistedstate'
export default createStore({
  state: {
    cartList:[], // 购物车的数据
  },
  mutations: {
    saveCart(state,val){
      let obj = {
        ...val,
        check: false,
        buyNum: 1
      }
      let index = state.cartList.findIndex(item => item.id == val.id);
      if (index>-1){
        state.cartList[index].buyNum++;
        return
      }
      state.cartList.push(obj);
    },
    checkAll(state,bool){ // 全选的函数
      state.cartList.forEach(item =>{
         item.check = bool
      })
    },
    delect(state){ // 批量删除的函数
      state.cartList = state.cartList.filter(item => item.check == false)
    },
    kong(){}
  },
  actions: {
  },
  modules: {
  },
  plugins: [
    new persist({
      storage: window.localStorage,
    }),
  ]
})
